/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeanAdmin;

import entity.Articles;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import managerBeanViews.Login;
import org.primefaces.event.DateSelectEvent;
import processor.ArticlesFacade;

/**
 *
 * @author Phong
 */
@ManagedBean(name = "articleMBean")
@SessionScoped
public class ArticleMBean implements Serializable{

    @ManagedProperty(value = "#{login}")//this is EL name of your bean 
    private Login injectedBean;
    @EJB
    private ArticlesFacade articlesFacade;
    private List<Articles> articles;
    private List<Articles> filteredArticles;
    private Articles selectedArticle = new Articles();
    private String articleTitle;
    private String articleContent;
    private Date createDate = new Date();
    private String navReturn = "editArticle.xhtml?faces-redirect=true";

    /**
     * Creates a new instance of ArticleMBean
     */
    public ArticleMBean() {
    }

    /**
     * @return the articles
     */
    public List<Articles> getArticles() {
        articles = articlesFacade.getArticlesAll();
        return articles;
    }

    public void createArticle(ActionEvent actionEvent) {
        FacesMessage msg;
        try {
            Calendar createCal = Calendar.getInstance();
            createCal.setTime(createDate);
            articleContent = articleContent.replaceAll("\\r|\\n", ""); 
            if (articlesFacade.createArticle(articleTitle, articleContent, createCal, getInjectedBean().getCustomer())) {
                msg = new FacesMessage("Created department: ", articleTitle + " successfully.");
            } else {
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN,"Created department: ", articleTitle + " failed.");
            }
        } catch (Exception e) {
            msg = new FacesMessage("Error: ", e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public String actionEditArticle() {
        String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("articleID");
        selectedArticle = articlesFacade.getArticlesByArticlesID(Integer.parseInt(id));
        return navReturn;
    }

    public void editArticle(ActionEvent actionEvent) {
        FacesMessage msg;
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(selectedArticle.getArticlesCreateDate());
            selectedArticle.setArticlesContent(selectedArticle.getArticlesContent().replaceAll("\\r|\\n", ""));
            if (articlesFacade.editArticle(selectedArticle.getArticlesId(), selectedArticle.getArticlesTitle(),
                    selectedArticle.getArticlesContent(), cal, getInjectedBean().getCustomer())) {
                msg = new FacesMessage("Edited department: ", selectedArticle.getArticlesTitle() + " successfully.");
            } else {
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN,"Edited department ", selectedArticle.getArticlesTitle() + " failed.");
            }
        } catch (Exception e) {
            msg = new FacesMessage("Error: ", e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void removeArticle(ActionEvent actionEvent) {
        FacesMessage msg;
        try {
            if (articlesFacade.removeArticle(selectedArticle.getArticlesId())) {
                msg = new FacesMessage("Article removed: ", selectedArticle.getArticlesTitle() + " successfully.");
            } else {
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN,"Article removed: ", selectedArticle.getArticlesTitle() + " failed.");
            }
        } catch (Exception e) {
            msg = new FacesMessage("Error: ", e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void handleDateSelect(DateSelectEvent event) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected", format.format(event.getDate())));
    }

    public String getErrorMessage(final String clientId) {
        if (clientId.equals("form:articleTitle")) {
            return "Title is required and betwwen 3 to 200 letters";
        } else if (clientId.equals("form:articleContent")) {
            return "Content is required";
        } else {
            Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages(clientId);
            if (iter.hasNext()) {
                return iter.next().getDetail(); // or getSummary()
            }
            return "";
        }
    }

    /**
     * @param articles the articles to set
     */
    public void setArticles(List<Articles> articles) {
        this.articles = articles;
    }

    /**
     * @return the filteredArticles
     */
    public List<Articles> getFilteredArticles() {
        return filteredArticles;
    }

    /**
     * @param filteredArticles the filteredArticles to set
     */
    public void setFilteredArticles(List<Articles> filteredArticles) {
        this.filteredArticles = filteredArticles;
    }

    /**
     * @return the selectedArticle
     */
    public Articles getSelectedArticle() {
        return selectedArticle;
    }

    /**
     * @param selectedArticle the selectedArticle to set
     */
    public void setSelectedArticle(Articles selectedArticle) {
        this.selectedArticle = selectedArticle;
    }

    /**
     * @return the articleTitle
     */
    public String getArticleTitle() {
        return articleTitle;
    }

    /**
     * @param articleTitle the articleTitle to set
     */
    public void setArticleTitle(String articleTitle) {
        this.articleTitle = articleTitle;
    }

    /**
     * @return the articleContent
     */
    public String getArticleContent() {
        return articleContent;
    }

    /**
     * @param articleContent the articleContent to set
     */
    public void setArticleContent(String articleContent) {
        this.articleContent = articleContent;
    }

    /**
     * @return the createDate
     */
    public Date getCreateDate() {
        return createDate;
    }

    /**
     * @param createDate the createDate to set
     */
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    /**
     * @return the navReturn
     */
    public String getNavReturn() {
        return navReturn;
    }

    /**
     * @param navReturn the navReturn to set
     */
    public void setNavReturn(String navReturn) {
        this.navReturn = navReturn;
    }

    /**
     * @return the injectedBean
     */
    public Login getInjectedBean() {
        return injectedBean;
    }

    /**
     * @param injectedBean the injectedBean to set
     */
    public void setInjectedBean(Login injectedBean) {
        this.injectedBean = injectedBean;
    }
}
